﻿// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITEE: https://gitee.com/AntdUI/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename
{
    public partial class Form3 : Form
    {
        private ListView listView1;
        private int customHeaderHeight = 40; // 自定义列头高度
        public Form3()
        {
            InitializeComponent();
            InitializeListView();
            this.Text = "修复 ListView 文字不显示问题";
            this.Size = new Size(600, 400);
        }
        private void InitializeListView()
        {
            listView1 = new ListView();
            listView1.Dock = DockStyle.Fill;
            listView1.View = View.Details;

            // 关键设置：必须非Default
            listView1.HeaderStyle = ColumnHeaderStyle.Clickable;

            // 添加列
            listView1.Columns.Add("ID", 80);
            listView1.Columns.Add("名称", 200);
            listView1.Columns.Add("状态", 150);

            // 添加示例数据
            for (int i = 0; i < 5; i++)
            {
                listView1.Items.Add(new ListViewItem(new[]
                {
                i.ToString(),
                $"项目 {i + 1}",
                i % 2 == 0 ? "启用" : "禁用"
            }));
            }

            // 启用所有者绘制
            listView1.OwnerDraw = true;
            listView1.DrawColumnHeader += ListView1_DrawColumnHeader;
            listView1.DrawSubItem += ListView1_DrawSubItem;
            listView1.DrawItem += ListView1_DrawItem;

            this.Controls.Add(listView1);
        }
        // 绘制列头
        private void ListView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            // 列头绘制区域（使用自定义高度）
            Rectangle headerRect = new Rectangle(
                e.Bounds.X,
                e.Bounds.Y,
                e.Bounds.Width,
                customHeaderHeight
            );

            // 绘制背景
            using (Brush brush = new SolidBrush(Color.LightGray))
            {
                e.Graphics.FillRectangle(brush, headerRect);
            }

            // 绘制列头文字（居中）
            StringFormat format = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };
            e.Graphics.DrawString(
                e.Header.Text,
                new Font("宋体", 9, FontStyle.Bold),
                Brushes.Black,
                headerRect,
                format
            );

            // 绘制底部边框（分隔列头和内容）
            e.Graphics.DrawLine(
                Pens.Red,
                headerRect.Left,
                headerRect.Bottom - 1,
                headerRect.Right,
                headerRect.Bottom - 1
            );
        }
        // 绘制子项（核心：正确计算内容区域）
        private void ListView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            // 计算子项区域：从列头下方开始绘制
            int itemTop = customHeaderHeight + (e.Item.Index * e.Item.Bounds.Height);
            Rectangle subItemRect = new Rectangle(
                e.Bounds.X,
                itemTop,  // 关键：Y坐标从列头高度开始
                e.Bounds.Width,
                e.Item.Bounds.Height
            );

            // 交替行背景
            Color backColor = (e.Item.Index % 2 == 0) ? Color.White : Color.FromArgb(240, 240, 240);
            e.Graphics.FillRectangle(new SolidBrush(backColor), subItemRect);

            // 绘制子项文字（确保在可视区域内）
            StringFormat format = new StringFormat
            {
                LineAlignment = StringAlignment.Center,
                Alignment = e.ColumnIndex == 0 ? StringAlignment.Near : StringAlignment.Center
            };
            e.Graphics.DrawString(
                e.SubItem.Text,
                e.Item.Font,
                Brushes.Black,
                subItemRect,
                format
            );
        }
        // 绘制项（处理选中状态）
        private void ListView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            if ((e.State & ListViewItemStates.Selected) != 0)
            {
                // 选中区域从列头下方开始
                Rectangle selectRect = new Rectangle(
                    e.Bounds.X,
                    customHeaderHeight + (e.Item.Index * e.Item.Bounds.Height),
                    e.Bounds.Width,
                    e.Item.Bounds.Height
                );
                e.Graphics.FillRectangle(new SolidBrush(Color.Red), selectRect);
            }
        }
    }
}
